const app = getApp()
var xrSdk = require('../../../vendor/hope-sdk/index.js');
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
const util = require('../../../utils/util.js');
const qrCode = require('../../../libs/weapp-qrcode.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    webURL: config.service.imageUrl,
    id    : 0,
    order : null,
    first : true,
    showQrcode:false,
    showC:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that   = this;
    const W    = wx.getSystemInfoSync().windowWidth;
    const rate = 750.0 / W;
    //利用比例将260rpx转换为px
    const qrcode_w = 320 / rate;
    this.setData({
      id: options.id || 0,
      qrcode_w: qrcode_w
    }, () => {
      that.init();
    });
  },
  init:function(){
    var that = this;
    if (that.data.id > 0) {
      xrRequest.request(app,{
        url: config.service.orderDetail,
        login: true,
        data: {
          id: that.data.id
        },
        success: function (res) {
          if (res.data.code == 1) {
            that.setData({
              order: res.data.data
            });
            that.couponQrCode(res.data.data.code);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        },
        fail: function (res) { },
        authfail: function (e) {
          wx.navigateTo({
            url: "../../auth/auth"
          })
        }
      })
    }
  },
  // 二维码生成工具
  couponQrCode(text) {
    new qrCode('couponQrcode', {
      text: text,
      width: this.data.qrcode_w,
      height: this.data.qrcode_w,
      colorDark: "#333333",
      colorLight: "#FFFFFF",
      correctLevel: qrCode.CorrectLevel.H
    });
  },
  edit:function(){
    var that = this;
    var order_id = that.data.id;
    app.WxService.navigateTo("../edit/edit", {
      order_id: order_id,
      tyre_id : that.data.order.tyre.id
    });
  },
  submit: function (e) {
    var that = this;
    var order_id = that.data.id;
    wx.showModal({
      title: '确定提交吗',
      content: '确定提交本次鼓包更换申请吗？',
      success(res) {
        if (res.confirm) {
          that.submitHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  submitHandler: function (order_id) {
    var that = this;
    xrRequest.request(app,{
      url: config.service.submitOrder,
      login: true,
      auth: true,
      data: {
        order_id: order_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.init();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
       },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  delete: function (e) {
    var that = this;
    var order_id = that.data.id;
    wx.showModal({
      title: '确定删除吗',
      content: '确定删除本次鼓包更换申请吗？',
      success(res) {
        if (res.confirm) {
          that.deleteHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  deleteHandler: function (order_id) {
    var that = this;
    xrRequest.request(app,{
      url: config.service.deleteOrder,
      login: true,
      auth: true,
      data: {
        order_id: order_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  sure: function (e) {
    var that = this;
    var order_id = that.data.id;
    wx.showModal({
      title: '确定完成吗',
      content: '确定新轮胎已经安装完成吗？',
      success(res) {
        if (res.confirm) {
          that.sureHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  sureHandler: function (order_id) {
    var that = this;
    xrRequest.request(app,{
      url: config.service.sureOrder,
      login: true,
      auth: true,
      data: {
        order_id: order_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.init();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  showQrcode:function(e){
    this.setData({
      showQrcode: true
    });
    setTimeout(() => {
      this.setData({
        showC: true
      })
    }, 0);
  },
  hideQrcode:function(){
    this.setData({
      showQrcode: false,
      showC : false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!this.data.first){
      this.init();
    }
    this.setData({
      first:false
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})